﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.GUI;

namespace SchoolManagementSystem
{
    public partial class LogInForm : Form
    {
        public LogInForm()
        {
            InitializeComponent();
        }

        private void linkLblPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RecoveryForm passRecover = new RecoveryForm();
            passRecover.Show();
        }

        private void cmdQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cmdLogIn_Click(object sender, EventArgs e)
        {
            string position = (String)cmBoxPosition.SelectedItem;
            if (position == null)
            {
                MessageBox.Show("You must select your position");
                cmBoxPosition.Focus();
                return;
            }
            else
            {
                loginTableAdapter.ClearBeforeFill = true;
                loginTableAdapter.FillByAll(sMSD_BaseDataSet.Login, txtUsername.Text, txtPassword.Text);
                if (sMSD_BaseDataSet.Login.Count == 0)
                {
                    Invalid invalidForm = new Invalid();
                    invalidForm.Show();
                    txtUsername.Clear();
                    txtPassword.Clear();
                }
                else
                {
                    loginTableAdapter.ClearBeforeFill = true;
                    string userID = (String)loginTableAdapter.getUserID(txtUsername.Text, txtPassword.Text);
                    string firstLetters = userID.Substring(0, 2);
                    switch (position)
                    {
                        case "Teacher":
                            TeacherHome teacherHome = new TeacherHome();
                            teacherHome.Show();
                            break;

                        case "Student":
                            StudentHome studentHome = new StudentHome();
                            studentHome.Show();
                            break;

                        case "Clerk":

                            break;

                        case "Principal":
                            if (firstLetters != "PR")
                            {
                                MessageBox.Show("You do not have access to this account");
                                return;
                            }
                            PrincipalHome principalHome = new PrincipalHome();
                            principalHome.setUserID(userID);
                            principalHome.Show();
                            break;
                    }
                    this.Hide();
                }
            }


        }

    }
}
